//
//  CYBettingView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/30.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTableView.h"

@interface CYTableView()

@property (nonatomic, strong) UITableView  * tableView;
@property (nonatomic, strong) UIView *backView;//被遮挡的视图
@property (nonatomic, assign) NSInteger rowNum;//cell的行数
@property (nonatomic, assign) CGFloat cellHeight;//cell的高度
@property (nonatomic, assign) CGFloat headerHeight;//头视图的高度
@end

@implementation CYTableView
-(id)initWithBounds:(CGRect)bounds backGroundView:(UIView *)view dataSource:(id<CYTableViewDataSource>)dataSource delegate:(id<CYTableViewDelegate>)delegate{
    self = [super init];
    if (self){
        // Initialization code
        self.cy_tableView_dataSource = dataSource;
        self.cy_tableView_delegate = delegate;
        self.bounds = bounds;
        self.backView = view;
    }
    return self;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.contentSize = self.bounds.size;
        [self addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(self.bounds.size);
        }];
    }
    return _tableView;
}

- (void)setBackView:(UIView *)backView{
    if (_backView != backView)
        _backView = backView;
    [self addSubview:_backView];
    
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.centerX.equalTo(self);
        make.size.mas_equalTo(_backView.bounds.size);
    }];
}

#pragma mark CYTableViewProtocol Methods
-(void)reloadData{
 [self.tableView reloadData];
    
    CGFloat height = MIN(_cellHeight * _rowNum + _headerHeight, self.bounds.size.height);
    [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, self.bounds.size.height));
    }];
    
    [_backView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_backView.bounds.size);
    }];
    
//    _tableView.contentOffset = CGPointMake(0, _backViewHeight);
    
}

-(NSIndexPath*)reconstructIndexPath:(NSIndexPath*)indexPath{
    return [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section + 1];
}
#pragma mark UITableViewDataSource Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(!section)
        return 1;
    _rowNum = [_cy_tableView_dataSource cy_tableView:tableView numberOfRowsInSection:section - 1];
    return _rowNum;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [_cy_tableView_dataSource numberOfSectionsInCYTableView:tableView] + 1;
}

static NSString * identify = @"IdentifyForTableView";
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(!indexPath.section) {
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identify];
        if(!cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            cell.backgroundColor             = [UIColor clearColor];
            cell.contentView.backgroundColor = [UIColor clearColor];
            cell.selectionStyle              = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    else
        return [_cy_tableView_dataSource cy_tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section + 1]];
    
}

#pragma mark UITableViewDelegate Methods
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSAssert(_backViewHeight > 0, @"第一个cell的高度_backViewHeight不能等于0");
    if(!indexPath.section)
        return _backViewHeight;
    NSIndexPath *indexPathParallax  = [self reconstructIndexPath:indexPath];
    _cellHeight = [_cy_tableView_delegate cy_tableView:tableView heightForRowAtIndexPath:indexPathParallax];
    return _cellHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(!section)
        return 0;
    _headerHeight = [_cy_tableView_delegate cy_tableView:tableView
                                heightForHeaderInSection:section-1];
    return _headerHeight;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(!section)
        return nil;
    return [_cy_tableView_delegate cy_tableView:tableView
                         viewForHeaderInSection:section-1];
}

@end
